package welcome.general;

import general.AspectLibraryException;
import grammar.GrammarEditingScreen;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;

import propertiesSelection.PropertySelectionScreen;

import development.PropertyCreationScreen;

public class ChoosePerspective {

	private static JFrame frame;
	private static ChoosePerspective window = null;
	
	public static final String NAME_APP = "Aspect Library";
	public static final String VERSION = "v. 1.0";
	
	private JButton btnGrammar, btnDevelopment, btnProperties;
	
	/* To accelerate the frame building. When need this screen - setVisible(true); */
	/* Impacts: The same frame is displayed. All actions will be remain. */
	private static GrammarEditingScreen grammarEditingScreen = new GrammarEditingScreen();
	private static PropertyCreationScreen propertyCreationScreen = new PropertyCreationScreen();
	private static PropertySelectionScreen propertySelectionScreen = new PropertySelectionScreen();
	
	public static JFrame getChooseAppScreen() {
		return frame;
	}

	public static ChoosePerspective getWindow() {
		return window;
	}

	public static void load() {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					window = new ChoosePerspective();
					window.frame.setVisible(true);
				} catch (Exception e) {
					JOptionPane.showMessageDialog(null, e.getStackTrace(), AspectLibraryException.INTERNAL_ERROR, JOptionPane.ERROR_MESSAGE);
				}
			}
		});
	}

	public ChoosePerspective() {
		initialize();
	}
	
	private void hasColorOnGrammarButton(boolean value){
		if(value){
			btnGrammar.setIcon(new ImageIcon("C:\\Users\\F. OMENA\\Dropbox\\Mestrado\\Projeto-Mestrado\\AspectLibrary\\Images\\notepad.png"));
		}else{
			btnGrammar.setIcon(new ImageIcon("C:\\Users\\F. OMENA\\Dropbox\\Mestrado\\Projeto-Mestrado\\AspectLibrary\\Images\\notepad_gray.png"));
		}
	}
	
	private void hasColorOnPropertiesButton(boolean value){
		if(value){
			btnProperties.setIcon(new ImageIcon("C:\\Users\\F. OMENA\\Dropbox\\Mestrado\\Projeto-Mestrado\\AspectLibrary\\Images\\mark.png"));
		}else{
			btnProperties.setIcon(new ImageIcon("C:\\Users\\F. OMENA\\Dropbox\\Mestrado\\Projeto-Mestrado\\AspectLibrary\\Images\\mark_gray.png"));
		}
	}
	
	private void hasColorOnDevelopmentButton(boolean value){
		if(value){
			btnDevelopment.setIcon(new ImageIcon("C:\\Users\\F. OMENA\\Dropbox\\Mestrado\\Projeto-Mestrado\\AspectLibrary\\Images\\books.png"));
		}else{
			btnDevelopment.setIcon(new ImageIcon("C:\\Users\\F. OMENA\\Dropbox\\Mestrado\\Projeto-Mestrado\\AspectLibrary\\Images\\books_gray.png"));
		}
	}
	
	private void openGrammarScreen(){
		grammarEditingScreen.getFrame().setVisible(true);
		
		frame.setVisible(false);
	}
	
	private void openLibraryScreen(){
		propertyCreationScreen.getFrame().setVisible(true);
		
		frame.setVisible(false);
	}
	
	private void openPropertyScreen(){
		propertySelectionScreen.populateTable();
		
		propertySelectionScreen.getFrame().setVisible(true);
		
		frame.setVisible(false);
	}

	private void initialize() {
		/* Frame settings. */
		frame = new JFrame();
		frame.getContentPane().setBackground(new Color(245, 245, 245));
		frame.setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\F. OMENA\\Dropbox\\Mestrado\\Projeto-Mestrado\\AspectLibrary\\Images\\logo\\logo3.png"));
		/* Set frame attributes */
		frame.setResizable(false);
		frame.setTitle(NAME_APP + " " + VERSION);
		frame.setBounds(100, 100, 463, 218);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		/* Set frame position screen */
		frame.setLocation(
				((Toolkit.getDefaultToolkit().getScreenSize().width / 2) - (frame
						.getWidth() / 2)), ((Toolkit.getDefaultToolkit()
						.getScreenSize().height / 2) - (frame.getHeight() / 2)));
		
		/* Choose perspective */
		JLabel lblChooseAPerspective = new JLabel("Choose a perspective:");
		lblChooseAPerspective.setFont(new Font("Times New Roman", Font.PLAIN, 13));
		lblChooseAPerspective.setForeground(Color.BLACK);
		
		/* Grammar: */
		JLabel lblGrammar = new JLabel("Grammar Editing");
		lblGrammar.setFont(new Font("Times New Roman", Font.PLAIN, 11));
		
		btnGrammar = new JButton("");
		btnGrammar.setIcon(new ImageIcon("C:\\Users\\F. OMENA\\Dropbox\\Mestrado\\Projeto-Mestrado\\AspectLibrary\\Images\\notepad_gray.png"));
		btnGrammar.setForeground(Color.BLACK);
		btnGrammar.setBackground(new Color(204, 204, 255));
		btnGrammar.setFont(new Font("Times New Roman", Font.PLAIN, 12));
		
		/* Events */
		btnGrammar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				openGrammarScreen();
			}
		});
		btnGrammar.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseExited(MouseEvent arg0) {
				hasColorOnGrammarButton(false);
			}
			@Override
			public void mouseReleased(MouseEvent e) {
				hasColorOnGrammarButton(false);
			}
		});
		btnGrammar.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseMoved(MouseEvent arg0) {
				hasColorOnGrammarButton(true);
			}
			@Override
			public void mouseDragged(MouseEvent e) {
				hasColorOnGrammarButton(true);
			}
		});
		
		/* Development: */
		JLabel lblProperties = new JLabel("Property Development");
		lblProperties.setFont(new Font("Times New Roman", Font.PLAIN, 11));
		
		btnDevelopment = new JButton("");
		btnDevelopment.setIcon(new ImageIcon("C:\\Users\\F. OMENA\\Dropbox\\Mestrado\\Projeto-Mestrado\\AspectLibrary\\Images\\books_gray.png"));
		btnDevelopment.setForeground(Color.BLACK);
		btnDevelopment.setBackground(new Color(204, 204, 255));
		btnDevelopment.setFont(new Font("Times New Roman", Font.PLAIN, 12));
		
		/* Events */
		btnDevelopment.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				openLibraryScreen();
			}
		});
		btnDevelopment.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseExited(MouseEvent arg0) {
				hasColorOnDevelopmentButton(false);
			}
			@Override
			public void mouseReleased(MouseEvent e) {
				hasColorOnDevelopmentButton(false);
			}
		});
		btnDevelopment.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseMoved(MouseEvent arg0) {
				hasColorOnDevelopmentButton(true);
			}
			@Override
			public void mouseDragged(MouseEvent e) {
				hasColorOnDevelopmentButton(true);
			}
		});
		
		/* Client: */
		JLabel lblDevelopment = new JLabel("Choice of Properties");
		lblDevelopment.setFont(new Font("Times New Roman", Font.PLAIN, 11));

		btnProperties = new JButton("");
		btnProperties.setIcon(new ImageIcon("C:\\Users\\F. OMENA\\Dropbox\\Mestrado\\Projeto-Mestrado\\AspectLibrary\\Images\\mark_gray.png"));
		btnProperties.setForeground(Color.BLACK);
		btnProperties.setBackground(new Color(204, 204, 255));
		btnProperties.setFont(new Font("Times New Roman", Font.PLAIN, 12));
		
		/* Events */
		btnProperties.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				openPropertyScreen();
			}
		});
		btnProperties.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseExited(MouseEvent arg0) {
				hasColorOnPropertiesButton(false);
			}
			@Override
			public void mouseReleased(MouseEvent e) {
				hasColorOnPropertiesButton(false);
			}
		});
		btnProperties.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseMoved(MouseEvent arg0) {
				hasColorOnPropertiesButton(true);
			}
			@Override
			public void mouseDragged(MouseEvent e) {
				hasColorOnPropertiesButton(true);
			}
		});
		
		/* Layout */
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(171)
							.addComponent(lblChooseAPerspective))
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(btnGrammar, GroupLayout.PREFERRED_SIZE, 148, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(btnDevelopment, GroupLayout.PREFERRED_SIZE, 148, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(btnProperties, GroupLayout.PREFERRED_SIZE, 148, GroupLayout.PREFERRED_SIZE))
							.addGroup(groupLayout.createSequentialGroup()
								.addGap(34)
								.addComponent(lblGrammar)
								.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(lblProperties)
								.addGap(52)
								.addComponent(lblDevelopment)
								.addGap(25))))
					.addContainerGap(1, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(5)
					.addComponent(lblChooseAPerspective)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnGrammar, GroupLayout.PREFERRED_SIZE, 139, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnDevelopment, GroupLayout.PREFERRED_SIZE, 139, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnProperties, GroupLayout.PREFERRED_SIZE, 139, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblGrammar)
						.addComponent(lblDevelopment)
						.addComponent(lblProperties))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		frame.getContentPane().setLayout(groupLayout);
	}
}

